package work.display
{
	import flash.display.Sprite;
	import flash.events.AsyncErrorEvent;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.ProgressEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.utils.ByteArray;
	
	/**
	 * 加载完成初始化事件 
	 * @eventType flash.events.Event.INIT
	 */
	[Bindable]
	[Event(name="init", type="flash.events.Event")]
	
	/**
	 * 错误事件 
	 * @eventType flash.events.ErrorEvent.ERROR
	 */
	[Bindable]
	[Event(name="error", type="flash.events.ErrorEvent")]
	
	public class LoadEFCVideo extends Sprite
	{
		private var _video:Video;
		private var loader:LoadEFC;
		private var nc:NetConnection;
		
		private var videoWidth:Number;
		private var videoHeight:Number;
		
		private var sendError:Boolean;
		/**
		 * 加载加密FLV视频 
		 * @param w 视频宽
		 * @param h 视频高
		 * @param sendVideoError 是否发送视频错误事件,如果设置为 true,例如当您的视频缺乏 metaData 和 XMPData 时候会发出错误消息
		 * 
		 */
		public function LoadEFCVideo(w:Number=320,h:Number=240,sendVideoError:Boolean=true)
		{
			
			super();
			videoWidth=w;
			videoHeight=h;
			sendError=sendVideoError;
			init();
		}
		
		public function get video():Video
		{
			return _video;
		}

		private function init():void
		{
			loader=new LoadEFC();
			loader.addEventListener(Event.COMPLETE,onLoaderEvent);
			loader.addEventListener(ProgressEvent.PROGRESS,onLoadProgressEvent);
		}
		
		/**
		 * 加载一个经过加密的FLV视频的EFC文件 
		 * @param uri 地址
		 * @param key 密匙
		 * 
		 */
		public function load(uri:String,key:ByteArray):void
		{
			loader.load(uri,key);
		}
		private function onLoaderEvent(e:Event):void
		{
			//loader.removeEventListener(Event.COMPLETE,onLoaderEvent);
			if(_video)
			{
				this.removeChild(_video);
			}
			if(nc)
			{
				nc.removeEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			}
			nc=new NetConnection();
			nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			
			nc.connect(null);
			
		}
		private function netStatusHandler(event:NetStatusEvent):void
		{
			switch (event.info.code)
			{
				case "NetConnection.Connect.Success" :
					connectStream();
					break;
				case "NetStream.Play.StreamNotFound" :
					trace("Stream not found! ");
					break;
			}
		}
		//appendBytes 之前必须 ns.play(null);
		private function connectStream():void
		{
			var ns:NetStream = new NetStream(nc);
						
			//ns.addEventListener(NetStatusEvent.NET_STATUS,onNetStarusHander);
			ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR,onAsynErrorHander);
			
			ns.play(null);
			ns.appendBytes(loader.contentData);
			
			_video=new Video(videoWidth,videoHeight);
			_video.attachNetStream(ns);
			//this.addChild(_video);
			this.dispatchEvent(new Event(Event.INIT));
		}
		private function onLoadProgressEvent(e:ProgressEvent):void
		{
			this.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS,false,false,e.bytesLoaded,e.bytesTotal));
		}
		
		private function onNetStarusHander(e:NetStatusEvent):void
		{
			var s:String="";
			for(var p:String in e.info)
			{
				s+=p+" = "+e.info[p]+"\n";
			}
			trace(s);
		}
		private function onAsynErrorHander(e:AsyncErrorEvent):void
		{
			if(sendError)
			this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,e.text,e.errorID));
		}
	}
}